package com.example.admin.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.admin.R;
import com.example.admin.SQLite.BaoMing;
import com.example.admin.SQLite.Complaint;
import com.example.admin.SQLite.Payment;
import com.example.admin.SQLite.SqLiteOpenHelper;
import com.example.admin.SQLite.dao.BaoMingDao;
import com.example.admin.Utils.GlideEngine;
import com.example.admin.adapter.AdminTab5Listdapter;
import com.example.admin.adapter.AdminTab5dapter;
import com.example.admin.bean.EventBean;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.orhanobut.hawk.Hawk;

import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;

public class Tab5Fragment extends Fragment {
    AdminTab5dapter tab5dapter;
    RecyclerView recyclerView;
    String imgPath;
    ImageView ivImage;
    BaoMingDao baoMingDao;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_tab5, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        baoMingDao = new BaoMingDao(getActivity());
        recyclerView = view.findViewById(R.id.recyclerView);
        tab5dapter = new AdminTab5dapter();
        recyclerView.setAdapter(tab5dapter);
        tab5dapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv4) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setTitle("是否执行此操作");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            baoMingDao.DeleteContacts(tab5dapter.getItem(position).id);
                            tab5dapter.remove(position);
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();
                } else if (view.getId() == R.id.tv5) {
                    BaoMing bean=tab5dapter.getItem(position);
                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    View views = getLayoutInflater().inflate(R.layout.dialog_tab5_list, null);
                    builder.setView(views);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                    TextView tvTitle=views.findViewById(R.id.tvTitle);
                    RecyclerView recyclerView=views.findViewById(R.id.recyclerView);
                    AdminTab5Listdapter mAdapter=new AdminTab5Listdapter();
                    recyclerView.setAdapter(mAdapter);
                    mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                        @Override
                        public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                            builder.setTitle("是否执行此操作");
                            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    mAdapter.remove(position);
                                    Hawk.put("EventBean" + bean.id, mAdapter.getData());
                                    String num = Hawk.get("EventBeannum" + bean.id);
                                    baoMingDao.updateComs(bean.id, "" + mAdapter.getData().size() + "/" + num);

                                    getdata();
                                }
                            });
                            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            });
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                    });
                    List<EventBean> list = new ArrayList<>();
                    if (Hawk.contains("EventBean" + bean.id)){
                        list=Hawk.get("EventBean" + bean.id);
                    }
                    mAdapter.setNewData(list);
                    tvTitle.setText("活动名称："+bean.title+"\n"+"报名人数："+bean.Num);
                }
            }
        });
        view.findViewById(R.id.tvSure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                View views = getLayoutInflater().inflate(R.layout.dialog_tab5, null);
                builder.setView(views);
                AlertDialog dialog = builder.create();
                dialog.show();
                EditText tvTitle = views.findViewById(R.id.tvTitle);
                EditText tvCont = views.findViewById(R.id.tvCont);
                EditText tvNum = views.findViewById(R.id.tvNum);
                Spinner sp1 = views.findViewById(R.id.spinner1);
                Spinner sp2 = views.findViewById(R.id.spinner2);
                Spinner sp3 = views.findViewById(R.id.spinner3);
                Spinner sp11 = views.findViewById(R.id.spinner11);
                Spinner sp12 = views.findViewById(R.id.spinner12);
                Spinner sp13 = views.findViewById(R.id.spinner13);
                ivImage = views.findViewById(R.id.ivImage);
                ivImage.setOnClickListener(v1 -> {
                    // 进入相册 以下是例子：用不到的api可以不写
                    PictureSelector.create(Tab5Fragment.this)
                            .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                            // .theme()//主题样式(不设置为默认样式) 也可参考demo values/styles下 例如：R.style.picture.white.style
                            .loadImageEngine(GlideEngine.createGlideEngine())
                            .maxSelectNum(1)// 最大图片选择数量 int
                            .minSelectNum(1)// 最小选择数量 int
                            .imageSpanCount(4)// 每行显示个数 int
                            .selectionMode(PictureConfig.SINGLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                            .previewImage(true)// 是否可预览图片 true or false
                            .isCamera(true)// 是否显示拍照按钮 true or false
                            .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                            .sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
                            .enableCrop(true)// 是否裁剪 true or false
                            .compress(true)// 是否压缩 true or false
                            .hideBottomControls(true)// 是否显示uCrop工具栏，默认不显示 true or false
                            .freeStyleCropEnabled(true)// 裁剪框是否可拖拽 true or false
                            .openClickSound(true)// 是否开启点击声音 true or false
                            .previewEggs(true)// 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中) true or false
                            .minimumCompressSize(100)// 小于100kb的图片不压缩
                            .synOrAsy(true)//同步true或异步false 压缩 默认同步
                            .rotateEnabled(true) // 裁剪是否可旋转图片 true or false
                            .scaleEnabled(true)// 裁剪是否可放大缩小图片 true or false
                            .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code

                });
                views.findViewById(R.id.tvSure).setOnClickListener(v1 -> {
                    String s1 = (String) sp1.getSelectedItem();
                    String s2 = (String) sp2.getSelectedItem();
                    String s3 = (String) sp3.getSelectedItem();
                    String s11 = (String) sp11.getSelectedItem();
                    String s12 = (String) sp12.getSelectedItem();
                    String s13 = (String) sp13.getSelectedItem();
                    String startData = s1 + s2 + s3;
                    String endData = s11 + s12 + s13;
                    String tit = tvTitle.getText().toString().trim();
                    String cont = tvCont.getText().toString().trim();
                    String Num = tvNum.getText().toString().trim();
                    BaoMing bean = new BaoMing();
                    bean.title = tit;
                    bean.content = cont;
                    bean.imgPath = imgPath;
                    bean.CreateData = startData + "-" + endData;
                    bean.Num = "0/" + Num;
               long id=     baoMingDao.addData(bean);
                    getdata();

                    List<EventBean> list = new ArrayList<>();
                    Hawk.put("EventBean" +  id, list);
                    Hawk.put("EventBeannum" + id, Num);
                    Log.e("大师傅大师傅", "EventBeannum" + bean.id+"onClick: "+Hawk.get("EventBeannum" + bean.id) );
                    dialog.dismiss();
                });
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        getdata();
    }

    private void getdata() {
        ArrayList<BaoMing> list = baoMingDao.FindWhereData();
        tab5dapter.setNewData(list);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<LocalMedia> images;
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {// 图片选择结果回调

                images = PictureSelector.obtainMultipleResult(data);

                if (images.get(0).isCompressed()) {
                    imgPath = images.get(0).getCompressPath();
                } else {
                    imgPath = images.get(0).getPath();
                }
                Glide.with(this).load(imgPath).into(ivImage);
                //selectList = PictureSelector.obtainMultipleResult(data);

                // 例如 LocalMedia 里面返回三种path
                // 1.media.getPath(); 为原图path
                // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的

            }
        }
    }
}